package server

import (
	"4-11zk/common/model/mysql"
	"4-11zk/common/pkg"
	"4-11zk/common/proto/user"
	"errors"
	"fmt"
	jwt "github.com/golangblogs/gojwt"
	"github.com/google/uuid"
	"gorm.io/gorm"
	"strconv"
)

func Login(req *user.LoginReq) (*user.LoginResp, error) {
	getUser, err := mysql.GetUser(req.Phone)
	if err != nil {
		return nil, errors.New("查询失败")
	}
	if getUser.Phone == "" {
		m := mysql.User{
			Model:    gorm.Model{},
			Phone:    req.Phone,
			Password: req.Password,
		}
		err := mysql.Create(&m)
		if err != nil {
			return nil, errors.New("注册成功")
		}
		coupon := mysql.Coupon{
			Model:         gorm.Model{},
			UserId:        int(getUser.ID),
			CouponName:    "满100减5优惠券",
			DiscountPrice: 5,
			Condition:     100,
		}
		err = mysql.CouponCreate(&coupon)
		if err != nil {
			return nil, errors.New("优惠券发放失败")
		}
	}
	if getUser.Password != req.Password {
		return nil, errors.New("用户密码错误")
	}
	claims := jwt.CustomClaims{ID: getUser.ID}
	token, err := jwt.NewJWT("2209a").CreateToken(claims)
	if err != nil {
		return nil, errors.New("token连接失败")
	}
	return &user.LoginResp{Greet: token}, nil
}

func PlanAdd(req *user.PlanAddReq) (*user.PlanAddResp, error) {
	goods, err := mysql.ShowByGoods(int(req.CouResId))
	if err != nil {
		return nil, errors.New("查询失败")
	}
	plan := mysql.Plan{
		Model:       gorm.Model{},
		UserId:      int(req.UserId),
		CouRseId:    int(req.CouResId),
		CouRseName:  goods.CouRseName,
		CouRsePrice: goods.CouRsePrice,
		CouResInter: goods.CouResInter,
	}
	err = mysql.CreatePlan(&plan)
	if err != nil {
		return nil, errors.New("添加失败")
	}
	return &user.PlanAddResp{Greet: "添加成功"}, nil
}

func PlanDel(req *user.PlanDelReq) (*user.PlanDelResp, error) {
	err := mysql.PlanDel(int(req.Id))
	if err != nil {
		return nil, errors.New("删除失败")
	}
	return &user.PlanDelResp{Greet: "删除成功"}, nil
}

func OrderAdd(req *user.OrderAddReq) (*user.OrderAddResp, error) {
	goods, err := mysql.ShowByGoods(int(req.GoodsId))
	if err != nil {
		return nil, errors.New("课程查询失败")
	}
	newString := uuid.NewString()
	order := mysql.Order{
		Model:       gorm.Model{},
		UserId:      int(req.UserId),
		GoodsId:     int(req.GoodsId),
		OrderSn:     newString,
		CouRseName:  goods.CouRseName,
		CouRsePrice: goods.CouRsePrice,
		CouResInter: goods.CouResInter,
		OrderStatus: 1,
	}
	err = mysql.CreateOrder(&order)
	if err != nil {
		return nil, errors.New("订单生成失败")
	}
	var f float64

	if goods.CouRsePrice > 100 {
		f = goods.CouRsePrice - 5
	} else {
		f = goods.CouRsePrice
	}
	float := strconv.FormatFloat(f, 'f', 2, 64)
	alipay := pkg.Alipay()
	pays := alipay.ALiPays(goods.CouRseName, newString, float)
	return &user.OrderAddResp{
		OrderSn: newString,
		Url:     pays,
	}, nil
}

func OrderCall(req *user.OrderCallReq) (*user.OrderCallResp, error) {
	fmt.Println("支付回调")
	fmt.Println(req.OrderSn)
	fmt.Println(req.Status)
	var status int
	if req.Status == "WAIT_BUYER_PAY" {
		status = 1
	} else if req.Status == "TRADE_CLOSED" {
		status = 2
	} else if req.Status == "TRADE_SUCCESS" {
		status = 3
	} else if req.Status == "TRADE_FINISHED" {
		status = 4
	}
	err := mysql.UpdateStatus(req.OrderSn, status)
	if err != nil {
		return nil, errors.New("状态修改失败")
	}
	return &user.OrderCallResp{Success: "修改成功"}, nil
}
